//*** This code is copyright 2003 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt

var xbAttachEvent = function(obj, evt, handler) {
	return false;
}
var xbGetEventElement = function(evt) {
	return null;
}
var xbFireEvent = function(obj, evtype, evt) {
	return false;
}

if(window.addEventListener) {
	xbAttachEvent = function(obj, evt, handler, capture) {
		if(!capture) capture = false;
		obj.addEventListener(evt, handler, capture);
		return true;
	}
	xbFireEvent = function(obj, evtype, evt) {
		return obj.dispatchEvent(evt);
	}
	xbGetEventElement = function(evt) {
		var elem = null;
		if(evt) elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
		return elem;
	}
} else if(window.attachEvent) {
	xbAttachEvent = function(obj, evt, handler) {
		return obj.attachEvent("on" + evt, handler);
	}
	xbFireEvent = function(obj, evtype, evt) {
		return obj.fireEvent("on" + evtype);
	}
	xbGetEventElement = function(evt) {
		var elem = null;
		evt = evt ? evt : (window.event ? window.event : null);
		if(evt) elem = evt.srcElement;
		return elem;
	}
} else {
	xbAttachEvent = function(obj, evt, handler) {
		obj["on" + evt] = handler;
		return true;
	}
	xbFireEvent = function(obj, evtype, evt) {
		obj["on" + evtype]();
		return true;
	}
}

function xbPreventDefault(evt) {
	if(!evt && window.event) evt = window.event;

	if(evt.preventDefault) evt.preventDefault();
	else evt.returnValue = false;
	return false;
}

/*
2003-2004 Bjoern Graf, b.gr@b-cage.net, http://b-cage.net/
*/

function xbGetRelativeByTag(obj, rel, tn) {
	tn = tn.toUpperCase();
	while(obj && (!obj.tagName || obj.tagName != tn))
		obj = obj[rel];
	return obj;
}

function xbGetOffsetXY(e) {
	for(var ox = e.offsetLeft, oy = e.offsetTop; e.offsetParent;
		e = e.offsetParent, ox += e.offsetLeft, oy += e.offsetTop);
	return {x: ox, y: oy};
}

var xbRulesCache = {};
function xbGetRule(sel) {
	function getRules(styleSheet) {
		var rules = styleSheet.cssRules || styleSheet.rules;
		if((rules && rules.length && rules[0].type == 3) || (styleSheet.imports && styleSheet.imports.length))
			rules = getRules((rules.length && rules[0].styleSheet) || styleSheet.imports[0]);
		return rules;
	}

	var s = sel.toLowerCase();
	if(!xbRulesCache[s]) {
		var found = false;
		for(var j = 0; j < document.styleSheets.length && !found; j++) {
			var rules = getRules(document.styleSheets[j]);
			for(var i = 0, l = rules.length; i < l && !found; i++) {
				var rule = rules[i];
				if(rule.selectorText && rule.selectorText.toLowerCase() == s) {
					xbRulesCache[s] = rule;
					found = true;
					break;
				}
			}
		}
	}
	return xbRulesCache[s];
}
